package org.hutb.community.controller;

import org.hutb.community.service.AlphaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;


/**
 * @ClassName AlphaController
 * @Description:
 * 入门测试
 *  当请求 /alpha/hello  时,向浏览器输出 hello world.
 *  其中 @ResponseBody表明不是跳转页面.
 *
 *
 * @Author xubo
 * @Date 2021/11/19 0:30
 */

@Controller
@RequestMapping("/alpha")
public class AlphaController {

    @Autowired
    private AlphaService alphaService;

    @RequestMapping("/data")
    @ResponseBody
    public String getData(){
        return alphaService.find();
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String sayHello(){
        return "Hello Spring World!";
    }


    @RequestMapping("/http")
    public void http(HttpServletRequest request, HttpServletResponse response){
        System.out.println(request.getMethod());
        System.out.println(request.getServletPath());
        System.out.println(request.getParameter("name"));
        //返回相应数据
        response.setContentType("text/html;charset=utf-8");
        try ( PrintWriter printWriter = response.getWriter();){
            printWriter.write("<h1>使用原生HttpServletRequest</h1>");
        }catch (IOException e){
            e.printStackTrace();
        }
    }


    //获取get请求
    //  /student?current=1&limit=20
    @RequestMapping(path = "/students",method = RequestMethod.GET)
    @ResponseBody
    public String getStudents(
            @RequestParam(name = "current",required = false,defaultValue = "1")int current,
            @RequestParam(name = "limit",required = false,defaultValue = "10")int limit
    ){
        System.out.println(current);
        System.out.println(limit);
        return "some students!";
    }


    @RequestMapping(value = "/student/{id}",method = RequestMethod.GET)
    @ResponseBody
    public String getStudent(@PathVariable("id")int id){
        System.out.println(id);
        return "find a student";
    }

    @RequestMapping(value = "/student",method = RequestMethod.POST)
    @ResponseBody
    public String saveStudent(String name,int age){
        //方法的形参名 只要和 html表单中的name值一致即可解析
        System.out.println(name);
        System.out.println(age);
        return "保存成功";
    }


    //响应html页面
    @RequestMapping(value = "/teacher",method = RequestMethod.GET)
    public ModelAndView getTeacher(){
        ModelAndView mav = new ModelAndView();
        mav.addObject("name","小明");
        mav.addObject("age",9);
        mav.setViewName("/demo/view");
        return mav;
    }

    @RequestMapping(value = "/school",method = RequestMethod.GET)
    public String getSchool(Model model){
        model.addAttribute("name","北京大学");
        model.addAttribute("age",100);


        return "/demo/view";
    }

    //相应json数据
    @RequestMapping(value = "/json",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getEmp(){
        Map<String,Object> map = new HashMap<>();
        map.put("name","徐波");
        map.put("age",15);
        map.put("school","湖南工商大学");
        return map;              //自动将map转成json返回给浏览器
    }

    //相应json数据
    @RequestMapping(value = "/jsons",method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String, Object>> getEmps(){
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String,Object> map = new HashMap<>();
        map.put("name","徐波");
        map.put("age",15);
        map.put("school","湖南工商大学");
        list.add(map);
        map = new HashMap<>();
        map.put("name","小米");
        map.put("age",25);
        map.put("school","湖南大学");
        list.add(map);
        return list;              //自动将map转成json返回给浏览器
    }


}
